<%--
  Created by IntelliJ IDEA.
  User: xiaox
  Date: 2024/12/28
  Time: 16:23
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <style>
        /* 整体页面布局，使内容垂直水平居中 */
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            margin: 20px;
            background-color: #b9def0;
        }

        /* 整体表单容器的样式 */
        .form-div {
            background-color: #fff;
            border: 1px solid #ccc;
            border-radius: 5px;
            box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
            padding: 20px;
            width: 300px;
            /*margin: 0 auto;*/
        }

        /* 注册内容区域的样式 */
        .reg-content {
            margin-bottom: 20px;
        }

        /* 标题样式 */
        .reg-content h1 {
            color: #333;
        }

        /* 链接样式 */
        .reg-content a {
            color: #007BFF;
            text-decoration: none;
            margin-left: 5px;
        }

        /* 表单标签样式 */
        form label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }

        /* 输入框样式 */
        form input[type="text"],
        form input[type="password"],
        form input[type="tel"]{
            width: 100%;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 3px;
            margin-bottom: 10px;
        }


        form input[type="num"]{
            width: 60%;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 3px;
            margin-bottom: 10px;
        }

        /* 错误消息样式 */
        .err_msg {
            color: red;
            font-size: 12px;
        }

        /* 使登录按钮与用户名输入框等宽 */
        form input[type="submit"] {
            width: 100%;
            background-color: #007BFF;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 3px;
            cursor: pointer;
        }

        .num-container {
            display: flex;
            align-items: center;
        }

        .num-container img {
            height: 32px; /* 根据输入框的高度调整 */
            margin-left: 10px; /* 图片与输入框之间的间距 */
            margin-bottom: 10px;

        }
        

        .tel-container input[type = "yzm"] {
            width: 25%;
            padding: 10px;
            left: 5px;
            border: 1px solid #ccc;
            border-radius: 3px;
            background-color: #007BFF;
            color: white;
            cursor: pointer;
            margin-bottom: 10px;
        }

    </style>
</head>
<body>
<div class="form-div">
    <div class="reg-content" style="text-align: center">
        <h1>欢迎注册</h1>
        <a href="login.jsp">已有账号？点击登录</a>
    </div>
        <form action="register" method="post" id="reg-form">
            <label for="username">用户名：</label>
            <input type="text" id="username" name="username"  placeholder="请输入用户名"required>
            <br>
            <span id="username_err" class="err_msg" style="display:none">用户名由长度1~12 单词字符组成</span>

            <label for="password">密码：</label>
            <input type="password" id="password" name="password" placeholder="请输入密码" required>
            <br>
            <span id="password_err" class="err_msg" style="display: none">密码由长度6~12 单词字符组成</span>

            <label for="tel">手机号：</label>
            <input type="tel" id="tel" name="tel"  placeholder="请输入手机号" required>
            <br>
            <span id="tel_err" class="err_msg" style="display: none">手机号码格式有误</span>

            <label for="num">验证码：</label>
            <div class="num-container">
            <input type="num" id="num" name="num" placeholder="请输入验证码" required>
            <img src="th.jpg">
            </div>
            <br>
            <span id="num_err" class="err_msg" style="display: none">验证码有误</span>
            
            <input type="submit" value="注册">

        </form>
    </div>
</body>
<script>
    //验证用户名
    var usernameInput = document.getElementById("username");
    usernameInput.onblur = checkUser;
    function checkUser(){
        var username = usernameInput.value.trim();
        //判断用户名是否符合规则：长度1~12 单词字符组成
        //正则表达式
        var reg = /^\w{1,12}$/;
        var flag= reg.test(username);
        if(flag){
            //符合规则
            document.getElementById("username_err").style.display = "none";
        }else{
            //不符合规则，将span标签显示出来，给出用户提示。
            document.getElementById("username_err").style.display = "";
        }
        return flag;
    }

    //验证密码
    var passwordInput = document.getElementById("password");
    passwordInput.onblur = checkPassword;
    function checkPassword(){
        var password = passwordInput.value.trim();
        var reg = /^\w{6,12}$/;
        var flag= reg.test(password);
        if(flag){
            document.getElementById("password_err").style.display = "none";
        }else{
            document.getElementById("password_err").style.display = "";
        }
        return flag;
    }

    //验证手机号
    const telInput = document.getElementById("tel");
    telInput.onblur = checkTel;
    function checkTel(){
        const tel = telInput.value.trim();
        const reg = /^[1]\d{10}$/;
        const flag = reg.test(tel);
        if(flag){
            document.getElementById("tel_err").style.display = "none";
        }else{
            document.getElementById("tel_err").style.display = "";
        }
        return flag;
    }

    // 验证验证码
    const numInput = document.getElementById('num');
    numInput.onblur = checknum;
    function checknum() {
        const num = numInput.value.trim();
        const reg = /^5964$/;
        const flag = reg.test(num);
        if (flag) {
            document.getElementById('num_err').style.display = "none";
        } else {
            document.getElementById('num_err').style.display = "";
        }
        return flag;
    }


    //验证表单
    //获取表单对象
    var regForm = document.getElementById("reg-form");
    //绑定onsubmit事件
    regForm.onsubmit = function () {
        //挨个判断每一个表单项是否都符合要求，如果有一个不合符，则返回false
        var flag = checkUser() && checkPassword() && checkTel() && checknum();
        return flag;
    }

</script>
</html>
